package com.daz.student.summary.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.net.aso.q;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.daz.common.publicDict.pojo.publicDictPojo;
import com.daz.student.information.pojo.StudentInfoPojo;
import com.daz.student.information.server.IStudentInfoSV;
import com.daz.student.pojo.studentPojo;
import com.daz.student.server.IStudentServer;
import com.daz.student.summary.pojo.SummaryPojo;
import com.daz.student.summary.server.ISummarySV;
import com.daz.teacher.teacherInfo.pojo.teacher;
import com.github.pagehelper.PageInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SummaryAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ISummarySV summarySV;

	private String summaryId;
	private String studentId;
	private String title;
	private String contentText;
	private String remark;
	private String visible;
	private String academicYear;
	private List<SummaryPojo> list;
	private SummaryPojo value;
	private Boolean success;
	private String msg;
	public String page;
	public String limit;
	public String query;
	private PageInfo<SummaryPojo> resultList;
	private File upload; 
    private String uploadFileName;
    private String Path;
    private String photoWidth;
    private String photoHeight;
	public String searchSummaryList(){
		int s=0;
		int l=0;
		if(page!=null){
			s = Integer.parseInt(page);
		}
		if(limit != null){
			l = Integer.parseInt(limit);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(studentId != null){
			map.put("studentId",studentId);
		}else{
			map.put("studentId",session.get("userId"));
		}
		if(visible != null){
			map.put("visible",visible);
		}
		if(query != null  && !"".equals(query)){
			map.put("query",query);
		}
		try {
			resultList = summarySV.searchSummaryList(map,s,l);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String insertSummary(){
		SummaryPojo value = new SummaryPojo();
		value.setTitle(title);
		value.setStudentId((String)ActionContext.getContext().getSession().get("userId"));
		value.setContentText(contentText);
		value.setState("U");
		value.setRemark(remark);
		value.setAcademicYear(academicYear);
		Date date = new Date();
		value.setCreateDate(date);
		Calendar calendar = new GregorianCalendar(); 
	    calendar.setTime(date); 
	    calendar.add(calendar.DATE,7);//把日期往后增加7天.
	    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		value.setModifyDateLimit(date);
		value.setVisible(visible);
		try {
			summarySV.insertSummary(value);
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String deleteSummary(){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("studentId", (String)ActionContext.getContext().getSession().get("userId"));
		map.put("summaryId", summaryId);
		try {
			summarySV.deleteSummary(map);
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String updateSummary() throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String, Object>();
		studentId = (String)ActionContext.getContext().getSession().get("userId");
		map.put("studentId", studentId);
		map.put("summaryId", summaryId);
		map.put("title", title);
		map.put("contentText", contentText);
		map.put("remark", remark);
		map.put("visible", visible);
		try{
			summarySV.updateSummary(map);
			this.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			this.setSuccess(false);
			this.setMsg("修改失败！");
		}
		return SUCCESS;
	}

	public String searchSummaryById()
			throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String, Object>();
		if(studentId != null){
			map.put("studentId",studentId);
		}else{
			map.put("studentId", (String)ActionContext.getContext().getSession().get("userId"));
		}map.put("summaryId", summaryId);
		value = summarySV.searchSummaryById(map);
		return SUCCESS;
	}

	public String summaryUpPhoto(){
		String realPath =  photoPath()+	uploadFileName;	
        if (upload != null) {
        	File pathFile = new File(photoPath());
    		if(!pathFile.isDirectory()){
    			pathFile.mkdir();
    		}
        	try { 
        		BufferedInputStream bis = new BufferedInputStream( 
                    new FileInputStream(upload)); 
        		BufferedOutputStream bos = null; 
                bos = new BufferedOutputStream(new FileOutputStream(realPath));
                byte[] buff = new byte[8192]; 
                for (int len = -1; (len = bis.read(buff)) != -1;) { 
                    bos.write(buff, 0, len); 
                } 
                bos.flush(); 
                bis.close(); 
                bos.close();
                this.setSuccess(true);
                this.setPath("announcementPhoto\\"+uploadFileName);
            } catch (Exception e) { 
                e.printStackTrace(); 
                this.setSuccess(false);               
            }
                                                                 
        } 
        else{
        	this.setSuccess(false);
        }
		return SUCCESS;
	}
	public String photoPath(){
		String imgpath = "\\announcementPhoto\\";
		String path = ServletActionContext.getServletContext().getRealPath("/");
		return path+imgpath;
	}
	public SummaryPojo getValue() {
		return value;
	}

	public void setValue(SummaryPojo value) {
		this.value = value;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getSummaryId() {
		return summaryId;
	}

	public void setSummaryId(String summaryId) {
		this.summaryId = summaryId;
	}

	public String getAcademicYear() {
		return academicYear;
	}

	public void setAcademicYear(String academicYear) {
		this.academicYear = academicYear;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContentText() {
		return contentText;
	}

	public void setContentText(String contentText) {
		this.contentText = contentText;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

	public List<SummaryPojo> getList() {
		return list;
	}

	public void setList(List<SummaryPojo> list) {
		this.list = list;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public PageInfo<SummaryPojo> getResultList() {
		return resultList;
	}

	public void setResultList(PageInfo<SummaryPojo> resultList) {
		this.resultList = resultList;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getQuery() {
		return query;
	}

	public String getVisible() {
		return visible;
	}

	public void setVisible(String visible) {
		this.visible = visible;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getPath() {
		return Path;
	}

	public void setPath(String path) {
		Path = path;
	}

	public String getPhotoWidth() {
		return photoWidth;
	}

	public void setPhotoWidth(String photoWidth) {
		this.photoWidth = photoWidth;
	}

	public String getPhotoHeight() {
		return photoHeight;
	}

	public void setPhotoHeight(String photoHeight) {
		this.photoHeight = photoHeight;
	}
	
	
	
}


